<?php
//通用的位置HTML替换 传入一个位置ID、返回替换好的HTML字符串
function htmlReplace($siteId,$shunxu) {
	global $wpdb;
	$replacedHtml = "";
	$displayCount;
	//数据库模板
	$siteHtml;
	//用于计数文章顺序编号
	$i = 0;
	
	$sql_site = "select shownum,site_html from  bw_template_site where id=$siteId";
	$result_site = $wpdb->get_results ( $sql_site );
	foreach ( $result_site as $row_site ) {
		$displayCount = $row_site->shownum;
		$siteHtml = $row_site->site_html;
	}
	//对数据表bw_article_content的sql查询语句
	$sql_cont = "select * from wp_nwkvhq_posts where site_id=$siteId Order By priority limit " . $displayCount;
	$result_cont = $wpdb->get_results ( $sql_cont );
	
	if($shunxu > 1){
		$huanposts = get_posts( "orderby=id&numberposts=$displayCount&cat=$nowcatid");
	}else if($shunxu > 3){
		$huanposts = get_posts( "orderby=post_date&numberposts=$displayCount&cat=$nowcatid");
	}else{
		$huanposts = get_posts( "orderby=title&numberposts=$displayCount&cat=$nowcatid");
	}
	 
	foreach ( $huanposts as $post ) {
		//文章顺序编号每次叠加增加1
		$i += 1;
		
		$new_html = $siteHtml;
		
		//文章标题
		$new_html = str_ireplace ( "{article_title}", $post->post_title , $new_html );
		
		//文章简介
		$new_html = str_ireplace ( "{article_intro}",mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 40,"…"), $new_html );
		
		//文章uri
		$new_html = str_ireplace ( "{article_uri}", get_permalink( $post->ID ), $new_html );
		
		//文章图片uri
		$new_html = str_ireplace ( "{article_img_uri}", get_first_image($post->post_content), $new_html );
		
		$replacedHtml .= $new_html;
	}
	
	return $replacedHtml;
}

//得到当前分类下文章总数
function wt_get_category_count($input = '') {
	global $wpdb;
	
	if ($input == '') {
		$category = get_the_category ();
		return $category [0]->category_count;
	} elseif (is_numeric ( $input )) {
		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
		return $wpdb->get_var ( $SQL );
	} else {
		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
		return $wpdb->get_var ( $SQL );
	}
}
function get_first_image($content) {
	$first_img = '';
	ob_start ();
	ob_end_clean ();
	$output = preg_match_all ( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches );
	$first_img = $matches [1] [0];
	if (empty ( $first_img )) { //Defines a default image
		$first_img = "http://www.uujog.com/wp-content/themes/beauty_women_thema/images/default.gif";
	}
	return $first_img;
}
 





























